<?php

namespace app\controllers;

use yii\web\Controller;
use EasyWeChat\Message\News;
use app\models\PromoAgent;

class WechatController extends Controller {
	public $enableCsrfValidation = false;
	// public $userinfo;
	public $userinfo = array (
			'id' => 'o5HMHwc0PtR_hDtQ0u4BpK8NkZeE',
			'nickname' => 'Bing大人',
			'avatar' => 'http://wx.qlogo.cn/mmopen/ajNVdqHZLLCTTQPUyhVsR2WTcvSaVmKXjDnkQYOMXicQribBYHKO1v6H9hAzZADagTdDYHP9CnkfGz0QXjTJtQCg/0' 
	);
	
	// 验证身份并返回用户对象
	public function check_session($call_back) {
		$oauth = \Yii::$app->wechat->app->oauth;
		$session = \Yii::$app->session;
		// 未登录Wechat
		if (empty ( $session ['user_wechat'] )) {
			$session ['wechat_target_url'] = \Yii::$app->request->url;
			$oauth->redirect ()->send ();
		}
		// 已经登录过Wechat
		$this->userinfo = $session ['user_wechat'];
	}
	public function actionEventCallback() {
		$server = \Yii::$app->wechat->app->server;
		$server->setMessageHandler ( function ($message) {
			
			// 回复订阅消息
			if ($message->MsgType == 'event' && $message->Event == 'SCAN') {
				
				// 二维码事件
				$news = new News ( [
						'title' => 'Hi，终于等到你，还好没错过！',
						'description' => "趣寻网为您提供专业留学、游学、旅游、运动生态保险服务！\n没错，我们只做细分、垂直领域！\n暖心、靠谱的大品牌定制保险产品定让您不虚此行！\n出国高保障，海外轻松游！\n无中间价，别家没有",
						'url' => 'http://m.funfind.cn/index/list?promo_code=' . str_replace ( 'qrscene_', '', $message->EventKey ),
						'image' => 'http://m.funfind.cn/image/wechat/slogan_index.png'
				] );
				return $news;
			}
			
			// 回复订阅消息
			if ($message->MsgType == 'event' && $message->Event == 'subscribe') {
				
				// 二维码事件
				// $news = new News ( [
				// 		'title' => 'Hi，终于等到你，还好没错过！',
				// 		'description' => "趣寻网为您提供专业留学、游学、旅游、运动生态保险服务！\n没错，我们只做细分、垂直领域！\n暖心、靠谱的大品牌定制保险产品定让您不虚此行！\n您还可直接留言给我们，由专家为您答疑解忧。\n出国高保障，海外轻松游！\n无中间价，别家没有",
				// 		'url' => 'http://m.funfind.cn/index/list?promo_code=' . str_replace ( 'qrscene_', '', $message->EventKey ),
				// 		'image' => 'http://m.funfind.cn/image/wechat/slogan_index.png'
				// ] );
				$news = "<a href='http://web.funfind.cn/qx'>Hi，欢迎关注出国服务优选平台——趣寻网！</a>\n\n".
                        "<a href='http://m.funfind.cn/index/list?code=FFAXLX_FFLXWY'>1、参加保障最全、价格最优的留学/游学/商旅保障计划！</a>\n\n".
                        "<a href='http://web.funfind.cn/details/'>2、雅思、托福等出国考试，无论通过都有现金红包！</a>\n\n".
                        "<a href='https://mp.weixin.qq.com/s/d91ZUyv03nBR_X5qAXW4sQ'>3、亮瞎眼！移民欧洲，只需5.5万欧元！</a>\n\n".
                        "<a href='http://web.funfind.cn/qx'>4、牛！世界500强企业，实业or就业，包你100%拿Offer!</a>\n\n".
                        "<a href='http://web.funfind.cn/home'>邀请好友拿红包，上不封顶！一起赚钱，一起飞！</a>";
				return $news;
			}
			// 回复测试消息
			if ($message->MsgType == 'text' && $message->Content == '诊断') {
				
				$news = new News ( [ 
						'title' => '趣寻微信诊断',
						'description' => '回调页面，用于诊断信息，点击进入',
						'url' => 'http://m.funfind.cn/wechat/diagnosis/',
						'image' => 'http://funfind.cn/images/img/title.png' 
				] );
				return $news;
			}
			return "您好啊！有什么问题就直接问我吧！我们都将及时提供专业的服务。";
		} );
		$response = $server->serve ();
		$response->send (); // 发送响应
	}
	// 公共回调接口
	public function actionOauthCallback() {
		$oauth = \Yii::$app->wechat->app->oauth;
		$user = $oauth->user ();
		$session = \Yii::$app->session;
		// 获取 OAuth 授权结果用户信息
		$user_wechat_info = $user->toArray ();
		$session ['user_wechat'] = $user_wechat_info;
		
		$targetUrl = empty ( $session ['wechat_target_url'] ) ? '/site/login' : $session ['wechat_target_url'];
		header ( 'location:' . $targetUrl ); // 跳转回原页面
	}
	// 默认微信主页
	public function actionIndex() {
		$this->layout = "wechat";
		$this->check_session ();
		$user = $this->userinfo;
		
		$promo_agent = PromoAgent::findOne ( \Yii::$app->request->get ( 'promo_code' ) );
		
		return $this->render ( 'home', [ 
				'user' => $user 
		] );
	}
	// 诊断页面
	public function actionDiagnosis() {
		$this->layout = "wechat";
		$this->check_session ();
		$user = $this->userinfo;
		return $this->render ( 'diagnosis', [ 
				'user' => $user 
		] );
	}
	
	// 诊断页面
	public function actionPromoAgent() {
		$this->layout = "wechat";
		$promo_agent = PromoAgent::findOne ( \Yii::$app->request->get ( 'promo_code' ) );
		return $this->render ( 'promo_agent', [ 
				'promo_agent' => $promo_agent 
		] );
	}
	
	// 诊断页面
	public function actionAboutus() {
		$this->layout = "wechat";
		$this->check_session ();
		$user = $this->userinfo;
		return $this->render ( 'aboutus', [
			'user' => $user 
		]);
	}
	
	
	//
	public function actionQrcodeForeverCreate($event_code) {
	$this->layout = "wechat";
	$qrcode = \Yii::$app->wechat->app->qrcode;
	$result = $qrcode->forever ( $event_code );
	echo 'url:'.$result->url."\n";
	echo 'ticket:'.$result->ticket;
	exit ();
	}
}